package com.menggudf.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.menggudf.entity.CaseHistoryImg;

@Repository
public class CaseHistoryImgDao{
    
    private static final Log log = LogFactory.getLog(CaseHistoryImgDao.class);

    @PersistenceContext(unitName = "medicalUnit")
    private EntityManager entityManager;

    public void persist(CaseHistoryImg transientInstance) {
        try {
            entityManager.persist(transientInstance);
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void remove(CaseHistoryImg persistentInstance) {
        try {
            entityManager.remove(persistentInstance);
        } catch (RuntimeException re) {
            log.error("remove failed", re);
            throw re;
        }
    }

    public CaseHistoryImg merge(CaseHistoryImg detachedInstance) {
        try {
            CaseHistoryImg result = entityManager.merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public CaseHistoryImg findById(int id) {
        try {
            CaseHistoryImg instance = entityManager.find(CaseHistoryImg.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public CaseHistoryImg findByCode(String code) {
        try {
            Query query = entityManager.createQuery("from CaseHistoryImg c where c.code=?1").setParameter(1, code);
            List<CaseHistoryImg> list = query.getResultList();
            if(list!=null && list.size()>0){
                return list.get(0);
            }else{
                return null;
            }
        } catch (Exception re) {
            log.error("findByCode fail",re);
            throw re;
        }
    }

    public List<String> getMyCaseHistoryImgs(
            String userCode,
            String roleCode,
            int pageSize,
            int offset) {
        try {
            Query query = entityManager.createQuery("select distinct c.code from CaseHistoryImg c where c.userCode=?1 and c.roleCode=?2 order by c.createdTime desc ")
                    .setParameter(1, userCode).setParameter(2, roleCode).setFirstResult(offset).setMaxResults(pageSize);
            return query.getResultList();
        } catch (Exception re) {
            log.error("getMyCaseHistoryImgs fail",re);
            throw re;
        }
    }

    public long getMyCaseHistoryImgsCount(String userCode, String roleCode) {
        try {
            Query query = entityManager.createQuery("select distinct c.code from CaseHistoryImg c where c.userCode=?1 and c.roleCode=?2 ")
                    .setParameter(1, userCode).setParameter(2, roleCode);
            return query.getResultList().size();
        } catch (Exception re) {
            log.error("getMyCaseHistoryImgsCount fail",re);
            throw re;
        }
    }

    public List<CaseHistoryImg> findListByCode(String code) {
        try {
            Query query = entityManager.createQuery("from CaseHistoryImg c where c.code=?1").setParameter(1, code);
            return query.getResultList();
        } catch (Exception re) {
            log.error("findListByCode fail",re);
            throw re;
        }
    }

}
